/**
 * Copyright (c) 2010-2011 julewa.com.  
 * All rights reserved. 
 * 
 * @author Huang Benhua
 * @date 2011-2-20
 * 
**/
package com.julewa.db;

import org.springframework.util.StringUtils;

public class StringTrimmer {
	private boolean trimPrefix = true;
	private boolean trimAny = false;
	private boolean trimSuffix = true;
	private boolean normalTrim = true;
	//
	public StringTrimmer(){}
	public StringTrimmer(boolean pre, boolean any, boolean suff){
		trimPrefix = pre;
		trimAny = any;
		trimSuffix = suff;
		normalTrim = trimPrefix && trimSuffix;
	}
	public StringTrimmer(DB.AUTOTRIM at){
		if(at == null)return;
		trimPrefix = at.prefix();
		trimAny = at.any();
		trimSuffix = at.suffix();
		normalTrim = trimPrefix && trimSuffix;
	}
	//
	public boolean isTrimPrefix(){return trimPrefix;};
	public boolean isTrimAny(){	return trimAny;};
	public boolean isTrimSuffix(){return trimSuffix;};
	//
	public String trim(String str){
		if(str == null)return str;
		if(trimAny){
			return StringUtils.trimAllWhitespace(str);
		}else if(normalTrim){
			return str.trim();
		}else if(trimPrefix){
			return StringUtils.trimLeadingWhitespace(str);
		}else{
			return StringUtils.trimTrailingWhitespace(str);
		}
	}
}
